package io.jee.medusa.orm.condition;

import java.util.HashMap;
import java.util.Map;


public class Condition {
	// 0-与; 1-或
	public static final Integer RELATION_AND = 0;
	public static final Integer RELATION_OR = 1;

	// 0-数值; 1-字符; 2-逻辑; 3-日期; 4-数组;
	public static final Integer VALUE_TYPE_NUMBER = 2;
	public static final Integer VALUE_TYPE_TEXT = 3;
	public static final Integer VALUE_TYPE_BOOL = 4;
	public static final Integer VALUE_TYPE_DATE = 5;
	public static final Integer VALUE_TYPE_ARRAY = 6;
	// 0-等于; 1-不等于; 2-大于; 3-小于; 4-大于等于; 5-小于等于; 6-模糊; 7-包含;
	public static final Integer OPERATION_TYPE_EQ = 7;
	public static final Integer OPERATION_TYPE_NEQ = 8;
	public static final Integer OPERATION_TYPE_GT = 9;
	public static final Integer OPERATION_TYPE_LT = 10;
	public static final Integer OPERATION_TYPE_GET = 11;
	public static final Integer OPERATION_TYPE_LET = 12;
	public static final Integer OPERATION_TYPE_LIKE = 13;
	public static final Integer OPERATION_TYPE_IN = 14;
	public static final Integer OPERATION_IS_NULL = 15;
	public static final Integer OPERATION_NOT_NULL = 16;
	
	
	private static Map<Integer, String> staticSourceMap = new HashMap<Integer, String>();
	
	static {
		staticSourceMap.put(0, " and ");
		staticSourceMap.put(1, " or ");
		staticSourceMap.put(7, " = ");
		staticSourceMap.put(8, " != ");
		staticSourceMap.put(9, " > ");
		staticSourceMap.put(10, " < ");
		staticSourceMap.put(11, " >= ");
		staticSourceMap.put(12, " <= ");
		staticSourceMap.put(13, " like ");
		staticSourceMap.put(14, " in ");
		staticSourceMap.put(15, " is null ");
		staticSourceMap.put(16, " is not null ");
	}

	// >>>>>>>>>>>>>>>>>>>>>>>业务方法区<<<<<<<<<<<<<<<<<<<<<<<<<<

	public static Condition onNumber(String key) {
		return initCondition(VALUE_TYPE_NUMBER, key);
	}

	public static Condition onText(String key) {
		return initCondition(VALUE_TYPE_TEXT, key);
	}

	public static Condition onBool(String key) {
		return initCondition(VALUE_TYPE_BOOL, key);
	}

	public static Condition onDate(String key) {
		return initCondition(VALUE_TYPE_DATE, key);
	}

	public static Condition onArray(String key) {
		return initCondition(VALUE_TYPE_ARRAY, key);
	}

	/**
	 * 等于
	 * @param value
	 * @return
	 */
	public Condition eq(Object value) {
		return setOperationtype(OPERATION_TYPE_EQ, value);
	}
	
	public Condition isNull() {
		return setOperationtype(OPERATION_IS_NULL, null);
	}
	
	public Condition notNull() {
		return setOperationtype(OPERATION_NOT_NULL, null);
	}

	/**
	 * 小于
	 * @param value
	 * @return
	 */
	public Condition lt(Object value) {
		return setOperationtype(OPERATION_TYPE_LT, value);
	}

	/**
	 * 大于
	 * @param value
	 * @return
	 */
	public Condition gt(Object value) {
		return setOperationtype(OPERATION_TYPE_GT, value);
	}

	/**
	 * 大于等于
	 * @param value
	 * @return
	 */
	public Condition get(Object value) {
		return setOperationtype(OPERATION_TYPE_GET, value);
	}

	/**
	 * 小于等于
	 * @param value
	 * @return
	 */
	public Condition let(Object value) {
		return setOperationtype(OPERATION_TYPE_LET, value);
	}

	public Condition like(Object value) {
		return setOperationtype(OPERATION_TYPE_LIKE, value);
	}

	public Condition in(Object value) {
		return setOperationtype(OPERATION_TYPE_IN, value);
	}

	private static Condition initCondition(Integer dataType, String key) {
		Condition condition = new Condition();
		ConditionValue conditionValue = new ConditionValue(key);
		conditionValue.setValueType(dataType);
		condition.setConditionValue(conditionValue);
		return condition;
	}

	private Condition setOperationtype(Integer opType, Object value) {
		ConditionValue conditionValue = this.getConditionValue();
		conditionValue.setValue(value);
		conditionValue.setOpType(opType);
		this.setConditionValue(conditionValue);
		return this;
	}

	/**
	 * 回去对应静态资源翻译码
	 * @param code
	 * @return
	 */
	public static String getStaticResource(Integer code) {
		return staticSourceMap.get(code);
	}
	
	// >>>>>>>>>>>>>>>>>>>>>>>私有属性区<<<<<<<<<<<<<<<<<<<<<<<<<<

	/**
	 * 连接类型 0-and 1-or
	 */
	private Integer relation;
	private Boolean first = false;
	private ConditionValue conditionValue;

	public Integer getRelation() {
		return relation;
	}

	public void setRelation(Integer relation) {
		this.relation = relation;
	}

	public void setConditionValue(ConditionValue conditionValue) {
		this.conditionValue = conditionValue;
	}

	public ConditionValue getConditionValue() {
		return conditionValue;
	}

	public Boolean isFirst() {
		return first;
	}

	public void setFirst(Boolean first) {
		this.first = first;
	}

}
